/**
 * 根据id查询节点
 * @param {String} id
 * @returns 节点对象
 */
 export const Qi = (id) => {
    return document.getElementById(id)
}
/**
 * 创建节点
 * @param {String} dom
 * @returns 返回节点对象
 */
export const Ne = (dom) => {
    return document.createElement(dom)
}

/**
 * 封装判断元素的偏移量
 * @param {Document} ele 需要判断的dom对象
 * @returns 返回元素的偏移量
*/
export const getOffsetSum = (ele) => {
    const box = ele.getBoundingClientRect(),
        body = document.body,
        docElem = document.documentElement
    // 获取页面的scrollTop,scrollLeft(兼容性写法)
    const scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop,
        scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft
    const clientTop = docElem.clientTop || body.clientTop,
        clientLeft = docElem.clientLeft || body.clientLeft
    const top = box.top + scrollTop - clientTop,
        left = box.left + scrollLeft - clientLeft
    return {
        // Math.round 兼容火狐浏览器bug
        top: Math.round(top),
        left: Math.round(left)
    }
}
/**
 * 判断元素是否显示在当前的容器内
 * @param {String} container 所存在的容器节点
 * @param {Array<Number>} element 需要判断是否存在容器的节点
 * @param {Number} i 判断是否需要返回页码的重要参数
 * @returns 返回boolean 判断传入的节点是否在容器内 若传i 则返回元素是否存在与对象页码否则只返回元素是否显示
*/
export const inViewport = async (container, element, i) => {
    const cWidth = container.clientWidth
    const cHeight = container.clientHeight
    const eWidth = element.clientWidth
    const eHeight = element.clientHeight
    const co = getOffsetSum(container)
    const eo = getOffsetSum(element)
    // console.log(co.top, eo.top, eHeight, cHeight)
    let flag
    if (eo.top <= cHeight / 3) {
        flag = i - 1
    }
    const below = () => {
        return co.top + cHeight <= eo.top
    }
    const right = () => {
        return co.left + cWidth <= eo.left
    }
    const above = () => {
        return co.top >= eo.top + eHeight
    }
    const left = () => {
        return co.left >= eo.left + eWidth
    }
    const b = !right() && !left() && !below() && !above()
    if (i) {
        return { b: b, f: flag }
    }
    return b
}
/**
 * @param {Function} fn 滚动完毕后调用的方法
 * 滚动延迟，滚动完毕后调用方法
 */
let scrollTimer
export const scrollDom = (fn) => {
    // binding.value(evt, el)
    clearTimeout(scrollTimer)
    scrollTimer = setTimeout(() => {
        fn()
    }, 100)
}